JPA Interview Questions and Answers
Related differences
Ques 41. How to fetch a record using NamedQuery?
public Category findByCategoryId(Long categoryId) {
try {
logger.info("Enter - findByCategoryId()");
Query lQuery = (Query) em.createNamedQuery("Category.findByCategoryId");
Integer intValue = categoryId.intValue();
lQuery.setParameter("categoryId", intValue);
Category category = (Category) lQuery.getSingleResult();
logger.info("Exit - findByCategoryId");
return category;
} catch (PersistenceException exception) {
logger.debug(exception.getCause().getStackTrace());
logger.error("CategoryDaoImpl::maxCategoryId()::REASON OF EXCEPTION=" + exception.getMessage() + exception.getCause());
} finally {
closeEntityManager();
}
}
Most helpful rated by users:
- What is ORM in JPA?
- What is an EntityManager?
- What is the difference between persistence.xml and hibernate.cfg.xml?
- What is an Entity?
- Why to use JPA?